#include "string/stringoutput.h"
#include "common/common.h"
#include <stdio.h>

namespace PILO
{
    namespace Core
    {
        namespace String 
        {
            size_t FormatOutputToConsole(const char * fmt, ...)
            {
                char _buffer[MC_LIMIT_FORMATOUTOUT_MAX_BUFFER]; 

                va_list args;
                va_start(args,fmt);
#               if defined(WINDOWS)
                _vsnprintf_s(_buffer, sizeof(_buffer), _TRUNCATE ,fmt, args);
#               else
                vsnprintf(_buffer, sizeof(_buffer), fmt, args);
#               endif
                va_end(args); 

#               if defined(WINDOWS)
                int ret = printf("%s", _buffer);
#               else
                int ret = wprintf(L"%s", _buffer);
#               endif

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);

            }

            size_t FormatOutputToConsole(const wchar_t * fmt, ...)
            {
                wchar_t _buffer[MC_LIMIT_FORMATOUTOUT_MAX_BUFFER]; 

                va_list args;
                va_start(args,fmt);
#               if  defined(WINDOWS)
                _vsnwprintf_s(_buffer, sizeof(_buffer)/sizeof(wchar_t), _TRUNCATE ,fmt, args);

#               else
                vswprintf(_buffer, sizeof(_buffer)/sizeof(wchar_t), fmt, args);
#               endif
                va_end(args); 


#               if defined(WINDOWS)
                int ret = wprintf_s(_buffer);	
#               else
                int ret = wprintf(L"%ls",_buffer);
#               endif

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);
            }

            size_t FormatOutputToString(char * buf, size_t len, const char * fmt, ...)
            {
                va_list args;
                INT32 ret;

                va_start(args, fmt);
#               if defined(WINDOWS)
                ret = _vsnprintf_s(buf, len, _TRUNCATE ,fmt, args);
#               else
                ret = vsnprintf(buf, len, fmt, args);
#               endif
                va_end(args);

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);
            }

            size_t FormatOutputToString(wchar_t * buf, size_t len, const wchar_t * fmt, ...)
            {
                va_list args;
                INT32 ret;

                va_start(args, fmt);
#               if defined(WINDOWS)
                ret = _vsnwprintf_s(buf, len, _TRUNCATE ,fmt, args);
#               else
                ret = vswprintf(buf,len,fmt,args);
#               endif
                va_end(args);

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);
            }

            size_t VaArgsFormat(char * buf, size_t len, const char * fmt, va_list args)
            {
                INT32 ret;
#               if defined(WINDOWS) || defined(WIN32)
                ret = _vsnprintf_s(buf, len, _TRUNCATE ,fmt, args);
#               else
                ret = vsnprintf(buf, len, fmt, args);

#	            endif

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);
            }


            size_t VaArgsFormat(wchar_t * buf, size_t len, const wchar_t * fmt, va_list args)
            {
                INT32 ret;
#               if defined(WINDOWS) || defined(WIN32)
                ret = _vsnwprintf_s(buf, len, _TRUNCATE ,fmt, args);
#               else
                ret = vswprintf(buf,len,fmt,args);
#	            endif

                if (ret < 0 ) {
                    return MC_INVALID_SIZE;
                }

                return static_cast<size_t>(ret);
            }

        } /* String */
    }
} /* PILO */
